//
//  FoncusOnGoodsViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/12/4.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "FoncusOnGoodsViewModel.h"
#import "FoncusOnGoodsTableViewCell.h"
#import "FoncusOnBottomView.h"

@interface FoncusOnGoodsViewModel ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView * selfTableView;
@property (nonatomic,strong)UIView * s_view;
@property (nonatomic,strong)FoncusOnBottomView * bottomView;
@end

@implementation FoncusOnGoodsViewModel

-(instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        self.s_view = superView;
        tableView.height = tableView.height - kHeightProportion_In_iPhoneTriploid(44);
        tableView.mj_footer.hidden = YES;
        tableView.mj_header.hidden = YES;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.allowsSelectionDuringEditing = YES;
        tableView.rowHeight = kHeightProportion_In_iPhoneTriploid(165);
        tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(5))];
        tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(10))];
        [superView addSubview:tableView];
        self.selfTableView = tableView;
        //
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(roleStateEditChange:)
                                                     name:@"EditAction"
                                                   object:nil];
        
    }
    return self;
}

- (void)roleStateEditChange:(NSNotification *)notification{
    NSArray * arr = notification.object;
    NSInteger index = [arr[0] integerValue];
    BOOL isSelect = [arr[1] boolValue];
    if (index == 0) {
        if (!isSelect) {
            self.bottomView = [[FoncusOnBottomView alloc] initWithFrame:CGRectMake(0, kY_With_View(self.selfTableView, 0), KScreenWidth, kHeightProportion_In_iPhoneTriploid(48)) inSuperView:self.s_view];
            self.bottomView.hidden = YES;
            [UIView animateWithDuration:0.25 animations:^{
                self.selfTableView.height = self.selfTableView.height - kHeightProportion_In_iPhoneTriploid(48);
            } completion:^(BOOL finished) {
                self.bottomView.y = kY_With_View(self.selfTableView, 0);
                self.bottomView.hidden = NO;
                [self.s_view layoutIfNeeded];
            }];
            [self.selfTableView setEditing:YES animated:YES];
        }else{
            if (!self.selfTableView.editing) {
                return;
            }
            [self editingConfima];
        }
    }else{
        if (!self.selfTableView.editing) {
            return;
        }
        [self editingConfima];
    }
}

-(void)editingConfima{//编辑结束
    [UIView animateWithDuration:0.25 animations:^{
        self.selfTableView.height = self.selfTableView.height + kHeightProportion_In_iPhoneTriploid(48);
        self.bottomView.height = 0.01;
        [self.s_view layoutIfNeeded];
    }];
    [self.bottomView removeFromSuperview];
    [self.selfTableView setEditing:NO animated:YES];
}


#pragma mark -- 数据请求
-(void)requestGoodsList{
    
}
#pragma mark --- UITableViewDataSource,UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FoncusOnGoodsTableViewCell * cell = [FoncusOnGoodsTableViewCell cellWithTableView:tableView];
    return cell;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    //编辑设置成自定义的必须把系统的设置为None
    return UITableViewCellEditingStyleNone;
}

@end
